<template>
  <div class="basic-layout">
    <div class="nav-site unfold">
      <!-- 系统LOGO -->
      <div class="logo">
        <img src="@/assets/imgs/logo.png" alt="管理平台">
        <span>管理平台</span>
      </div>
      <!-- 导航菜单 -->
      <el-menu :default-openeds="openeds"
        :default-active="activeMenu"
        router :unique-opened="true"
               background-color="#000" text-color="#fff" popper-effect="light"
        class="nav-menu">
        <el-sub-menu v-for="(menu) in userMenu" :index="menu.menu_uri">
          <template #title>
            <el-icon :class="menu.menu_icon"><component :is="menu.menu_icon"></component></el-icon>
            <span class="menu-title" >{{ menu.menu_name }}</span>
          </template>
          <el-menu-item-group v-if="menu.children && menu.children.length > 0" >
            <el-menu-item v-for="(menu2) in menu.children" :index="'/'+menu.menu_uri+'/'+menu2.menu_uri">
              <template #title>
                <el-icon :class="menu2.menu_icon"><component :is="menu2.menu_icon"></component></el-icon>
                <span class="menu-title" >{{ menu2.menu_name }}</span>
              </template>
            </el-menu-item>
          </el-menu-item-group>
        </el-sub-menu>
      </el-menu>
    </div>
    <div class="content-right unfold">
      <div class="nav-top">
        <div class="nav-left">
          <div class="bread">
            <BreadCrumb></BreadCrumb>
          </div>
        </div>
        <div class="user-info">
          <el-dropdown @command="handleLogout" trigger="click">
            <div class="user-link">
              <img src="@/assets/imgs/avatar.jpg" alt="管理员" style="width:32px;height:32px;border-radius: 50%;">
              <div style="padding-left:5px;padding-top:8px;">
                <span >{{userInfo.userName}}</span>
                <el-icon class="el-icon--right"><arrow-down /></el-icon>
              </div>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item key="phone">手机号：{{userInfo.userPhone}}</el-dropdown-item>
                <el-dropdown-item key="logout">退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <div class="wrapper">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script >
import BreadCrumb from './BreadCrumb.vue'

export default {
  name:'Home',
  components:{ BreadCrumb },
  data(){
    return {
      userInfo: {
        userName:'管理员',
        userPhone:'18275312594'
      },
      userMenu:[],
      activeMenu:'/system/menu',
      openeds:['/system'],
    }
  },
  mounted() {
    this.getLogin();
    this.getMenuList()
  },
  methods:{
    handleLogout(key){
      if (key == 'phone') return
      this.$api.exitLogin().then(res=>{
        if(res.code === 200){
          const token_key = localStorage.getItem('token_key');
          if(token_key){
            localStorage.removeItem(token_key);
            localStorage.removeItem('token_key');
            this.$router.push('/login');
          }
        }
      });
    },
    async getMenuList(){
      try {
        const res = await this.$api.getUserMenuList()
        //console.log(list);
        if(res.code === 200) {
          this.userMenu = res.data
        }
      } catch (error) {
        console.error(error)
      }
    },
    async getLogin(){
      try {
        const res = await this.$api.getLogin()
        if(res.code === 200){
          this.userInfo.userName = res.data.nickname;
          this.userInfo.userPhone = res.data.phone;
        }
        //console.log(list);
        //this.userMenu = list
      } catch (error) {
        console.error(error)
      }
    }
  }
}
</script>

<style lang="scss">
.basic-layout {
  position: relative;
  margin:-8px;padding:0;
  .nav-site {
    position: fixed;
    width: 200px;
    height: 100vh;
    background-color: #001529;
    color: #fff;
    overflow-y: auto;
    transition: width .5s;
    .logo {
      display: flex;
      align-items: center;
      font-size: 18px;
      height: 50px;
      img {
        margin: 0 16px;
        width: 32px;
        height: 31px;
      }
    }
    .nav-menu {
      height: calc(100vh - 50px);
      border-right: none;
    }
    // 合并
    &.fold{
      width: 64px;
    }
    // 展开
    &.unfold{
      width: 200px;
    }
  }
  .content-right {
    margin-left: 200px;
    // 合并
    &.fold{
      margin-left: 64px;
    }
    // 展开
    &.unfold{
      margin-left: 200px;
    }
    .nav-top {
      height: 50px;
      line-height: 50px;
      display: flex;
      justify-content: space-between;
      border-bottom: 1px solid #ddd;
      padding: 0 20px;
      .nav-left {
        display: flex;
        align-items: center;
        .menu-fold {
          margin-right: 15px;
          font-size: 18px;
        }
      }
      .user-info {
        .user-link {
          display: flex;flex-direction: row;
          justify-content: center;align-items:stretch;
          margin-top:10px;
          cursor: pointer;
        }
      }
    }
    .wrapper {
      padding:5px 0 0 5px;
      height: calc(100vh - 50px);
      .main-page {
        height: 100%;
      }
    }
  }
}
</style>
